package common

import (
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"github.com/mojocn/base64Captcha"

	"github.com/super-team/start-kit/handler/response"
	"github.com/super-team/start-kit/tools"
	"github.com/super-team/start-kit/tools/log"
)

//configJsonBody json request body.
type configJsonBody struct {
	Id            string
	CaptchaType   string
	VerifyValue   string
	DriverAudio   *base64Captcha.DriverAudio
	DriverString  *base64Captcha.DriverString
	DriverChinese *base64Captcha.DriverChinese
	DriverMath    *base64Captcha.DriverMath
	DriverDigit   *base64Captcha.DriverDigit
}

func Route(r *gin.RouterGroup, middlewares ...gin.HandlerFunc) {
	a := r.Group("")
	{
		a.GET("/getCaptcha", GetCaptcha)
	}
}

// @Summary 获取验证码
// @Description 获取验证码
// @Tags 基础API
// @Accept json
// @Success 200 {string} string	"{"code": 200, "message": "获取验证码成功"}"
// @Failure 500 {string} string	"{"code": -1, "message": "获取验证码失败"}"
// @Router /api/getCaptcha [get]
func GetCaptcha(ctx *gin.Context) {
	e := configJsonBody{}
	e.Id = uuid.New().String()
	e.DriverDigit = base64Captcha.DefaultDriverDigit
	driver := e.DriverDigit
	ca := base64Captcha.NewCaptcha(driver, tools.CaptchaStore)
	id, img, err := ca.Generate()
	if err != nil {
		log.Log.Err(err).Send()
		response.Error(ctx, "获取验证码失败")
		return
	}
	response.Success(ctx, "获取验证码成功", gin.H{
		"id":  id,
		"img": img,
	})
}
